app.controller('goodsDetailEditCtrl', ['$scope', '$state', '$stateParams', '$timeout', 'goodsDetailEditService', 'toaster', function ($scope, $state, $stateParams, $timeout, goodsDetailEditService, toaster) {
    $scope.skus = new Array();
    $scope.skuContents = new Array();
    $scope.goodsDetailEditService = goodsDetailEditService;

    $scope.brands;
    $scope.types;
    $scope.shop;

    $scope.weeks=[{name:'周一',state:false},{name:'周二',state:false},{name:'周三',state:false},{name:'周四',state:false},
        {name:'周五',state:false},{name:'周六',state:false},{name:'周日',state:false}];
    $scope.times=['0:00','0:30','1:00','1:30','2:00','2:30','3:00','3:30','4:00','4:30','5:00','5:30','6:00','6:30','7:00','7:30','8:00',
        '8:30','9:00','9:30','10:00','10:30','11:00','11:30','12:00','12:30','13:00','13:30','14:00','14:30','15:00','15:30','16:00',
        '16:30','17:00','17:30','18:00','18:30','19:00','19:30','20:00','20:30','21:00','21:30','22:00','22:30','23:00','23:30'];

    $scope.init = function () {
        $scope.queryShop();
        $scope.querybrands();
        $scope.queryType();
        $scope.queryPostageTemplate();
        $scope.findById();
    };


    //查询系统分类
    $scope.systemTypesArr=[];

    $scope.querySystemType = function () {
        goodsDetailEditService.querySystemType($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.systemTypes = result.data;
                    for(var i=0;i<$scope.systemTypes.length;i++){
                        var index=$scope.goodsDetailEditService.goods.systemTypeId.indexOf($scope.systemTypes[i].id);
                        if(index>-1){
                            $scope.systemTypesArr.push($scope.systemTypes[i]);
                        }
                    }
                    break;
                default:
                    break;
            }
        })
    };

    //查询技师姓名
    $scope.technicianArr=[];

    $scope.queryShopTechnician = function () {
        goodsDetailEditService.queryShopTechnician($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.technicians = result.data;
                    for(var i=0;i<$scope.technicians.length;i++){
                        var index=$scope.goodsDetailEditService.goods.technicianId.indexOf($scope.technicians[i].id);
                        if(index>-1){
                            $scope.technicianArr.push($scope.technicians[i]);
                        }
                    }
                    break;
                default:
                    break;
            }
        })
    };


    $scope.changeSystemType=function(obj){
        $scope.goodsDetailEditService.goods.systemTypeId=[];
        for(var i=0;i<obj.length;i++){
            $scope.goodsDetailEditService.goods.systemTypeId.push(obj[i].id);
        }
    };

    //
    $scope.changeSellType=function(type){
        if(type==0){
            goodsDetailEditService.goods.isOpenSell=0;
            goodsDetailEditService.goods.isOpenRebate=0;
            goodsDetailEditService.goods.profit=0;
        }else if(type==1){
            goodsDetailEditService.goods.isOpenSell=1;
            goodsDetailEditService.goods.isOpenRebate=0;
            goodsDetailEditService.goods.profit=$scope.shop.profit;
        }else if(type==2){
            goodsDetailEditService.goods.isOpenSell=0;
            goodsDetailEditService.goods.isOpenRebate=1;
            if($scope.shop.profit<26){
                goodsDetailEditService.goods.profit=26;
            }else{
                goodsDetailEditService.goods.profit=$scope.shop.profit;
            }
        }else if(type==3){
            goodsDetailEditService.goods.isOpenSell=1;
            goodsDetailEditService.goods.isOpenRebate=1;
            if($scope.shop.profit<26){
                goodsDetailEditService.goods.profit=26;
            }else{
                goodsDetailEditService.goods.profit=$scope.shop.profit;
            }
        }
    };



    //查询店铺信息
    $scope.queryShop = function () {
        goodsDetailEditService.queryShop($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.shop = result.data;
                    break;
                default:
                    break;
            }
        })
    };





    //查询品牌信息
    $scope.querybrands = function () {
        goodsDetailEditService.querybrands($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.brands = result.data;
                    break;
                default:
                    break;
            }
        })
    }



    //查询类型信息
    $scope.queryType = function () {
        goodsDetailEditService.queryType($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.types = result.data;
                    break;
                default:
                    break;
            }
        })
    }



    //查询运费模板
    $scope.queryPostageTemplate = function () {
        goodsDetailEditService.queryPostageTemplate($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.postageTemplate = result.data;
                    break;
                default:
                    break;
            }
        })
    }




    //查询商品信息
    $scope.id = $stateParams.id;
    $scope.goodsId = $stateParams.id;
    $scope.findById = function () {
        goodsDetailEditService.findById($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goodsDetailEditService.goods = result.data;

                    if(result.data.canReserve!=null || angular.isUndefined(result.data.canReserve)){
                        openDay(result.data.canReserve);
                    }


                    if ($scope.goodsDetailEditService.goods.status == 1) {
                        $scope.allowNotEdit = true;
                    }
                    $scope.querySystemType();
                    $scope.queryShopTechnician();
                    $scope.getAllTags();

                    break;
                case 646:
                    toaster.pop('waiting', "提示", result.message);
                    $state.go('app.404');
                    break;
                default:
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        })
    }

    //循环遍历日期展示
    var openDay=function(openDays){
        var days=openDays.split(',');
        for(var i=0;i<$scope.weeks.length;i++){
            var index=days.indexOf($scope.weeks[i].name);
            if(index>-1){
                $scope.weeks[i].state=true;
            }
        }
    };

    //修改商品
    var initGoods = {
        isOpenSku: 0,
        isFreeShipping: 0,
        isOpenMember: 0,
        isRecommend: 0,
        isOpenRebate: 0,
        isHelpPeasant: 0,
        postageType: 0,
        systemTypeId:[],
        isOpenSell:0,
        profit:0,
        postageWeight:0.000,
        tagIds:[],
        techniciansId:[]
    };

    $scope.updateState = false;

    $scope.updateGoods = function () {

        if($scope.roundLabel()){
            return;
        }

        if ($scope.updateState) {
            return;
        }

        $scope.updateState = true;

        //预约参数
        if(goodsDetailEditService.goods.type==4){
            goodsDetailEditService.goods.reserveTime=goodsDetailEditService.goods.startTime+'-'+goodsDetailEditService.goods.endTime;
            goodsDetailEditService.goods.canReserve=selectWeek();
        }else{
            delete goodsDetailEditService.goods.startTime;
            delete goodsDetailEditService.goods.endTime;
            delete goodsDetailEditService.goods.canReserve;
            goodsDetailEditService.goods.time='';
        }


        //参数体系
        goodsDetailEditService.goods.goodsLabelItems=roundLabelItems();

        goodsDetailEditService.updateGoods($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    if ($scope.goodsDetailEditService.goods.isOpenSku == 1) {
                        $scope.saveGoodsSku(result.data.id);
                    } else {
                        toaster.pop('success', "提示", "操作成功");
                        $scope.updateState = false;
                        $state.go('app.goodsDetailPictures', { goodsId: $scope.goodsId });
                    }
                    break;
                default:
                    $scope.updateState = false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    //修改周期
    var selectWeek=function(){
        var day='';
        for(var i=0;i<$scope.weeks.length;i++){
            if($scope.weeks[i].state){
                day+=$scope.weeks[i].name+',';
            }
        }
        var index=day.lastIndexOf(',');
        if(index>-1){
            day=day.substring(0,index);
        }
        return day;
    }

    //---参数区域块---
    $scope.addGoodslabelItems=function(){
        var goodsLabelItems=new Object();
        $scope.labels.push(goodsLabelItems);
    }

    //移除list
    $scope.removeGoodslabelItems=function(index){
        $scope.labels.splice(index, 1);
    }


    $scope.initLabel=function(item){
        item.showLabel=false;
        item.showLabelItems=false;
    }

    $scope.initLabelItems=function(item){
        item.showLabelItems=!item.showLabelItems;
        item.showLabel=false;
    }

    $scope.labels=new Array();

    $scope.createLabel=function(item){
        if(item.label.name=="" || !item.label.name){
            return;
        }
        $scope.addlabel(item);
        item.showLabel=false;
    }

    $scope.cancelLabel=function(item){
        item.label.name=null;
        item.showLabel=false;
    }


    $scope.createLabelItems=function(item){
        $scope.addlabelItems(item.labelItems);
        item.showLabelItems=false;
    }

    $scope.cancelLabelItems=function(item){
        item.labelItems.value=null;
        item.showLabelItems=false;
    }

    //改变区域
    $scope.changeLabel=function(em,item){
        item.labelItems=new Object();
        $scope.labelItemsList=em.childrens;
        item.showLabel=false;
        em.selected=false;
        item.label=em;
        item.labelItems.labelId=item.label.id;
    }

    $scope.changeLabelItems=function(em,item){
        item.labelItems=em;
        item.showLabelItems=false;
    }

    //
    var roundLabelItems=function(){
        var arr=new Array();
        for(var i=0;i<$scope.labels.length;i++){
            var obj=new Object();
            obj.labelId=$scope.labels[i].label.id;
            obj.labelItemsId=$scope.labels[i].labelItems.id;
            obj.sort=i;
            arr.push(obj);
        }

        return arr;
    }


    //
    var cancelAll=function(){
        for(var i=0;i<$scope.labels.length;i++){
            $scope.labels[i].showLabel=false;
            $scope.labels[i].showLabelItems=false;
        }
    }

    //整合labels
    $scope.integrationLabels=function(){
        var arr=new Array();

        for(var i=0;i<$scope.labels.length;i++){
            var id=$scope.labels[i].label.id;
        }
    }


    //添加标签参数
    $scope.addlabel=function(item){
        item.label.platform=0;
        item.label.systemTypeId=[];

        goodsDetailEditService.addlabel($scope,item.label).then(function(result){
            switch (result.status) {
                case 200:
                    item.label.id=result.data.id;
                    item.labelItems=new Object();
                    item.labelItems.labelId=item.label.id;
                    $scope.labelItemsList=new Array();
                    break;
                case 715:
                    alert("参数名字已经存在,请重新填写");
                    item.label=new Object();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }


    //添加标签参数价值
    $scope.addlabelItems=function(labelItems){
        goodsDetailEditService.addlabelItems($scope,labelItems).then(function(result){
            switch (result.status) {
                case 200:
                    labelItems.id=result.data.id;
                    break;
                case 717:
                    alert("参数值已经存在,请重新填写");
                    labelItems=new Object();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //获取全部属性参数
    $scope.getAll=function(){
        goodsDetailEditService.getAll($scope).then(function(result){
            switch (result.status) {
                case 200:
                    $scope.labelList=result.data;

                    for(var i=0;i<$scope.labelList.length;i++){
                        $scope.labelList[i].selected=true;
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.getAll();


    //获得已经保存的参数
    $scope.getItemsAll=function(){
        goodsDetailEditService.getItemsAll($scope).then(function(result){
            switch (result.status) {
                case 200:

                    pushArr(result.data);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.getItemsAll();



    //添加参数
    var pushArr=function(data){
        for(var i=0;i<data.length;i++){
            var item=new Object();

            var label=new Object();

            label.id=data[i].labelId;
            label.name=data[i].name;
            item.label=label;

            var labelItems=new Object();
            labelItems.labelId=data[i].labelId;
            labelItems.id=data[i].labelItemsId;
            labelItems.value=data[i].value;

            item.labelItems=labelItems;

            item.sort=data[i].sort;
            $scope.labels.push(item);

        }
    }

    //addLabels
    $scope.roundLabel=function(){
        for(var i=0;i<$scope.labels.length;i++){
            if( !$scope.labels[i].label || !$scope.labels[i].labelItems ||  $scope.labels[i].label.name==null || $scope.labels[i].labelItems.value==null){
                alert("请填写完整的参数");
                return true;
            }
        }
        return false;
    }

    $scope.changeTechnician=function(obj){
        goodsDetailEditService.goods.techniciansId = [];
        if(obj.length > 0) {
            for (var i = 0; i < obj.length; i++) {
                goodsDetailEditService.goods.techniciansId.push(obj[i].id);
            }
        }
    }

    //保存商品sku
    $scope.saveGoodsSku = function (goodsId) {

        goodsDetailEditService.saveGoodsSku($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $state.go('app.goodsDetailPictures', { goodsId: $scope.goodsId });
                    $scope.updateState = false;
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default:
                    $scope.updateState = false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });

    }


    $scope.$on('skuContents', function (event, data) {
        $scope.skuContents = data;
    });

    $scope.$on('skus', function (event, data) {
        $scope.skus = data;
        $scope.backSkus = data;
    });

    //开始sku 初始化
    $scope.openSku = function () {
        debugger;
        if ($scope.goodsDetailEditService.goods.isOpenSku == 1) {
            $scope.goodsDetailEditService.goods.price = 0.00;
            $scope.goodsDetailEditService.goods.stock = 0;
            $scope.skus = $scope.backSkus;
        } else if ($scope.goodsDetailEditService.goods.isOpenSku == 0) {
            $scope.skus = [];
        }
    };


    //查询标签
    $scope.getAllTags=function(){
        goodsDetailEditService.getAllTags($scope).then(function(result){
            switch (result.status) {
                case 200:
                    $scope.tags=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    };

    //选择标签
    $scope.chooseItem=function(item,$event){
        if($event.target.checked){
            goodsDetailEditService.goods.tagIds.push(item.id);
        }else{
            var index=goodsDetailEditService.goods.tagIds.indexOf(item.id);
            if(index!=-1){
                goodsDetailEditService.goods.tagIds.splice(index,1);
            }
        }
    }

    $scope.checkTags=function(id){
        var index=goodsDetailEditService.goods.tagIds.indexOf(id);
        if(index!=-1){
            return true;
        }
        return false;
    }

}])